package com.ftg.learn.bus.service.impl;

import com.ftg.learn.bus.entity.Director;
import com.ftg.learn.bus.mapper.DirectorMapper;
import com.ftg.learn.bus.service.IDirectorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 负责人表 服务实现类
 * </p>
 *
 * @author lalala
 * @since 2022-03-07
 */
@Service
public class DirectorServiceImpl extends ServiceImpl<DirectorMapper, Director> implements IDirectorService {

    @Resource
    DirectorMapper directorMapper;

    /**
     * 查询车的信息和负责人的信息
     * @return data
     */
    @Override
    public List<Map<String, Object>> showDirectorBusInfo() {
        return directorMapper.showDirectorBusInfo() ;
    }


    /**
     * 根据负责人查询其负责的车的信息
     * @param did 负责人id
     * @return data
     */
    @Override
    public List<Map<String,Object>> showDirectorBusInfoByDid(Long did) {
        return directorMapper.showDirectorBusInfoByDid(did);
    }


    /**
     * 逻辑删除负责人
     * 修改公车信息的负责人
     * @param did 删除id
     * @param bid id 车id
     * @param newDid 修改的负责人id
     * @return true
     */
    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean updateBusInfoDirectorByBid(Long did,Long bid,Long newDid) {
        this.removeById(did);
        directorMapper.updateBusInfoByBid(newDid,bid);
        return true;
    }


    /**
     * 逻辑删除负责人  修改公车信息的负责人
     * @param newDid id
     * @param bid id
     * @return true
     */
    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean updateBusInfoDidDeleteDirectorByBid(Long newDid,Long bid) {
        Long delDid = directorMapper.getBusInfoDid(bid);
        this.removeById(delDid);
        directorMapper.updateBusInfoByBid(newDid,bid);
        return true;
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public boolean updateInfoDid(Long did, Long newDid) {

        List<Long> list = directorMapper.showBid(did);
        for (Long id : list){
            directorMapper.updateDid(newDid,id);
        }
        return true;
    }
}
